package com.suixuan.support.service.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suixuan.support.dao.hibernate.BaseDaoImpl;
import com.suixuan.support.service.IBaseService;

/**
 * 
 * @Copyright (c) Nanjing Zhou Su co.,Ltd
 * 
 * @Project zhousuframework
 * @Title BaseServiceImpl.java
 * @Package com.suixuan.support.service.impl
 * @Description 业务接口实现
 * @Author suixuan
 * @Editors 1
 * @Date 2010-12-1 下午01:07:04
 * @Note
 * @Version:1.0.0
 * @param <D>
 *            数据库的接口类型
 * @param <T>
 *            实体类型
 * @param <PK>
 *            主键类型
 * 
 */
@Service
public abstract class BaseServiceImpl<D extends BaseDaoImpl<T, PK>, T, PK extends Serializable>
		implements IBaseService<T, PK> {
	protected D baseDaoImpl;

	private static final long serialVersionUID = 3517971366048092338L;

	protected abstract void setBaseDao(D baseDaoImpl);

	@Override
	public T findById(PK id) {

		return this.baseDaoImpl.find(this.getEntityClass(), id);
	}

	@SuppressWarnings("unchecked")
	private Class<T> getEntityClass() {
		return (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[1];
	}

	/**
	 * 保存
	 * 
	 * @Title save
	 * @Description
	 * @param entity
	 *            {@link entity}
	 * @throws UniquePropertyException
	 *             违反unique唯一约束异常
	 * 
	 */
	@Override
	@Transactional
	public void save(T entity) {
		try {
			this.baseDaoImpl.save(entity);
		} catch (Exception e) {
		}
	}

	@Override
	@Transactional
	public void saveOrUpdate(T entity) {
		try {
			this.baseDaoImpl.saveOrUpdate(entity);
		} catch (Exception e) {
		}
	}

	/**
	 * 保存
	 * 
	 * @Title save
	 * @Description
	 * @param entity
	 *            {@link List}
	 * @throws UniquePropertyException
	 *             违反unique唯一约束异常
	 * 
	 */
	@Override
	@Transactional
	public void batchSave(List<T> entity) {
		try {
			this.baseDaoImpl.batchSave(entity);
		} catch (Exception e) {
		}
	}

	/**
	 * 删除
	 * 
	 * @Title delete
	 * @Description
	 * @param entity
	 *            {@link T}
	 * 
	 */
	@Override
	@Transactional
	public void delete(T entity) {
		this.baseDaoImpl.delete(entity);
	}

	/**
	 * 删除
	 * 
	 * @Title delete
	 * @Description
	 * @param entityClass
	 *            类型
	 * @param id
	 *            主键
	 * 
	 */
	@Override
	@Transactional
	public void delete(Class<T> entityClass, PK id) {
		this.baseDaoImpl.delete(entityClass, id);
	}

}
